<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\WxpayTmd;
class Order extends Base{
    private $appid='wx897a77a94be25594';
    private $mch_id='1607321461';
    private $apikey='Sylvia19881988198819881988198819';
    public function orderIn(){
        if(!empty(input("param.homestay_title"))){
            $homestay_title=input("param.homestay_title");
        }else{
            return json(['code' => 510, 'msg' => '房源名称']);
        }
        if(!empty(input("param.date"))){
            $data['date']=input("param.date");
        }else{
            return json(['code' => 511, 'msg' => '入住日期']);
        }
        if(!empty(input("param.tdate"))){
            $data['tdate']=input("param.tdate");
        }else{
            return json(['code' => 512, 'msg' => '离租日期']);
        }
        if(!empty(input("param.alldays"))){
            $data['alldays']=input("param.alldays");
        }else{
            return json(['code' => 513, 'msg' => '共几晚']);
        }
        if(!empty(input("param.openid"))){
            $data['openid']=input("param.openid");
        }else{
            return json(['code' => 501, 'msg' => '未授权']);
        }
        if(!empty(input("param.h_id"))){
            $data['h_id']=input("param.h_id");
        }else{
            return json(['code' => 502, 'msg' => '请重新选择房间']);
        }
        if(!empty(input("param.hd_id"))){
            $data['hd_id']=input("param.hd_id");
        }else{
            return json(['code' => 503, 'msg' => '请重新选择租房日期']);
        }
        if(!empty(input("param.name"))){
            $data['name']=input("param.name");
        }else{
            return json(['code' => 504, 'msg' => '请输入姓名']);
        }
        if(!empty(input("param.phone"))){
            $data['phone']=input("param.phone");
        }else{
            return json(['code' => 505, 'msg' => '请输入手机号']);
        }
        if(!empty(input("param.arrive_time"))){
            $data['arrive_time']=input("param.arrive_time");
        }else{
            return json(['code' => 506, 'msg' => '请选择到达时间']);
        }
        if(!empty(input("param.payfee"))){
            $data['payfee']=input("param.payfee");
        }else{
            return json(['code' => 507, 'msg' => '支付金额？']);
        }
        $is_later=Db::name('homestay')->where('state','<>',1)->where('id',input("param.h_id"))->find();
        if($is_later){
            return json(['code' => 508, 'msg' => '您晚了一步，此房已关闭']);
        }
        $is_rent=Db::name('homestay_date')->where('id','in',input("param.hd_id"))->where('is_rent','<>',1)->whereOr('state','<>',1)->select();
        if($is_rent){
            return json(['code' => 509, 'msg' => '您晚了一步，请重新选租房源和日期']);
        }
        $data['orderno']=date('YmdHis',time()).mt_rand(1111,9999);
        $data['rooms']=1;
        $data['state']=1;
        $data['state_info']=1;
        $data['time']=time();
        $data['uptime']=time();
        $data['paytime']=time();
        Db::startTrans();
        try {
            $orderId=Db::name('orders')->insertGetId($data);
            Db::name('homestay_date')->where('id','in',input("param.hd_id"))->where('is_rent',1)->update(['is_rent'=>2,'uptime'=>time()]);
            // 提交事务
            Db::commit();
            if($orderId){
                $weixinpay = new WxpayTmd($this->appid,$data['openid'],$this->mch_id,$this->apikey,$data['orderno'],$homestay_title,floatval($data['payfee'] * 100),$this->request->domain());
                $return=$weixinpay->pay();
                return json(['code' => 200, 'msg' => '成功！','data'=>$return]);
            }else{
                return json(['code' => 500, 'msg' => '提交事物失败！']);
            }
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
    }
    public function notify()
    {
        $input = file_get_contents('php://input');
        $pay_result = simplexml_load_string($input, 'SimpleXMLElement',LIBXML_NOCDATA);
        if($pay_result->result_code=='SUCCESS'&&$pay_result->return_code=='SUCCESS'){
            $paystate=Db::name('orders')->where('orderno',$pay_result->out_trade_no)->find();
            if($paystate['state']==1){
                Db::name('orders')->where('orderno',$pay_result->out_trade_no)->update(['state'=>2,'uptime'=>time(),'paytime'=>time()]);
            }
        }
    }
    public function myOrders(){
        if(!empty(input("param.openid"))){
            $openid=input("param.openid");
        }else{
            return json(['code' => 501, 'msg' => '未授权']);
        }
        if(!empty(input("param.state"))){
            $state=input('get.state');
        }else{
            return json(['code' => 502, 'msg' => '状态呢']);
        }
        if(!empty(input("param.page"))){
            $page=input('get.page');
        }else{
            return json(['code' => 503, 'msg' => '页面呢']);
        }
        $map = [
            ['a.openid', '=', $openid]
        ];
        if($state==0){
            array_push($map,['a.state','<',6]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        // $res=Db::name('orders')->where($map)->order('id desc')->limit(10*($page-1),10)->select();
        $res=Db::name('orders')->alias('a')->join('homestay b', 'a.h_id = b.id','LEFT')->where($map)->field('a.*,b.des,b.bed,b.storey,b.area,b.breakfast,b.peoples,b.wifi')->order('a.id desc')->limit(10*($page-1),10)->select();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查询失败！']);
        }
    }
	// 下单
	public function orderInxx()
	{
		if(!empty(input("param.openid"))){
            $data['openid']=input("param.openid");
        }else{
            return json(['code' => 501, 'msg' => '未授权']);
        }
		if(!empty(input("param.payfee"))){
			$data['payfee']=input("param.payfee");
		}else{
			return json(['code' => 502, 'msg' => '支付金额？']);
		}
		if(input("param.remark")){
			$data['remark']=input("param.remark");
		}
		$data['orderno']=date('YmdHis',time()).mt_rand(1111,9999);
		$data['state']=1;
		$data['time']=time();
		$data['uptime']=time();
		$data['paytime']=time();

		Db::startTrans();
        try {
			$orderId=Db::name('orders')->insertGetId($data);
			// 提交事务
            Db::commit();
            if($orderId){

	        $weixinpay = new WxpayTmd($this->appid,$data['openid'],$this->mch_id,$this->apikey,$data['orderno'],'当面付款',floatval($data['payfee'] * 100),$this->request->domain());
			$return=$weixinpay->pay();
                return json(['code' => 200, 'msg' => '成功！','data'=>$return]);
            }else{
                return json(['code' => 500, 'msg' => '提交事物失败！']);
            }
		} catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '事物回滚失败！']);
        }
	}
    public function notifyxx()
    {
    	$input = file_get_contents('php://input');
        $pay_result = simplexml_load_string($input, 'SimpleXMLElement',LIBXML_NOCDATA);
        if($pay_result->result_code=='SUCCESS'&&$pay_result->return_code=='SUCCESS'){
            $paystate=Db::name('orders')->where('orderno',$pay_result->out_trade_no)->find();
            if($paystate['state']==1){
                Db::name('orders')->where('orderno',$pay_result->out_trade_no)->update(['state'=>2,'uptime'=>time(),'paytime'=>time()]);
            }
        }
    }
  	
}